<script setup lang="ts">
  import { ref, defineEmits, computed, watch, onMounted } from 'vue';
  import { useBooleanStore } from '@/store';
  import { searchToolMaterial } from '@/api/system-basic';

  const emit = defineEmits(['updateArray']);
  const booleanStore = useBooleanStore();
  // 表单查询数据
  const palletCode = ref<string>('');
  const packageCode = ref<string>('');
  const aoCode = ref<string>('');
  const itemCode = ref<string>('');

  const pageSize = ref<number>(10);
  const pageNumber = ref<number>(1);
  // 发送搜索数据
  const flowQueryData = ref({
    searchParams: computed(() => ({
      palletCode: palletCode.value,
      packageCode: packageCode.value,
      aoCode: aoCode.value,
      itemCode: itemCode.value,
    })),
    pageParameter: computed(() => ({
      rows: pageSize.value,
      page: pageNumber.value,
    })),
  });
  // 表格数据

  async function handleSearch() {
    const newData = await searchToolMaterial(flowQueryData.value);
    emit('updateArray', newData);
  }
  // 重置
  function reset() {
    palletCode.value = '';
    packageCode.value = '';
    aoCode.value = '';
    itemCode.value = '';
    handleSearch();
  }
  onMounted(() => {
    handleSearch();
  });
  // 监听分页
  watch([() => booleanStore.pageNum, () => booleanStore.pageSize], () => {
    if (booleanStore.num5 === 42) {
      pageNumber.value = booleanStore.pageNum;
      pageSize.value = booleanStore.pageSize;
      handleSearch();
    }
  });
  watch(
    () => booleanStore.update,
    () => {
      if (booleanStore.num6 === 23) handleSearch();
    }
  );
</script>

<template>
  <a-row :gutter="{ md: 8, lg: 24, xl: 32 }">
    <a-col :span="6">
      <div>
        <a-input
          v-model="palletCode"
          placeholder="工作包器具编码"
          size="large"
        />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="packageCode" placeholder="工作包编码" size="large" />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="aoCode" placeholder="AO任务号" size="large" />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="itemCode" placeholder="物料编码" size="large" />
      </div>
    </a-col>
  </a-row>

  <div class="btn-group">
    <a-button type="primary" size="large" @click="handleSearch">查询</a-button>
    <a-button class="btn-reset" size="large" @click="reset">重置</a-button>
    <a-button
      type="primary"
      class="btn-reset"
      size="large"
      @click="booleanStore.toggleBool(9)"
      >新增</a-button
    >
  </div>
</template>

<style scoped>
  .btn-group {
    float: right;
    margin-top: 15px;
  }

  .btn-reset {
    margin-left: 10px;
  }
</style>
